package com.taiji.dicom4cloud.smart.alarm.socket;

import com.taiji.dicom4cloud.common.core.utils.SpringContextHolder;
import com.taiji.dicom4cloud.smart.alarm.common.EnumHandler;
import com.taiji.dicom4cloud.smart.alarm.manager.IMessageAlarmService;
import com.taiji.dicom4cloud.smart.alarm.model.Message;
import com.taiji.dicom4cloud.smart.alarm.utils.MessageUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

/**
 * @Description
 * @Author fanxl
 * @Date 2021/8/16
 **/
@Slf4j
public class ClientHandler implements Runnable {

    private Socket socket;

    ClientHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        DataInputStream dataInputStream = null;
        try {
            dataInputStream = new DataInputStream(socket.getInputStream());
            byte[] bytes = new byte[128];
            dataInputStream.read(bytes);
            log.info("消息：{}",new String(bytes));
            Message message = new Message();
            message.setMessageId(MessageUtils.getShortId(bytes));
            message.setEquipmentId(new String(MessageUtils.getEnquipmentId(bytes)));
            message.setData(MessageUtils.getData(bytes));
            IMessageAlarmService iMessageStrategyManagerService = SpringContextHolder.getBean(IMessageAlarmService.class);
            if (MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.START)) {
                message.setCode(EnumHandler.START.getType());
                message.setMessageName(EnumHandler.START.name());
                iMessageStrategyManagerService.handle(message);
            } else if (MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.NORMAL)) {
                message.setCode(EnumHandler.NORMAL.getType());
                iMessageStrategyManagerService.handle(message);
            } else if (MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.ALARM)
                    || MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.REPORT_ALARM)) {
                message.setCode(EnumHandler.ALARM.getType());
                message.setMessageName(EnumHandler.ALARM.name());
                iMessageStrategyManagerService.handle(message);
            } else if (MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.BREAK_DOWN)) {
                message.setCode(EnumHandler.BREAK_DOWN.getType());
                message.setMessageName(EnumHandler.BREAK_DOWN.name());
                iMessageStrategyManagerService.handle(message);
                iMessageStrategyManagerService.handle(message);
            } else if (MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.ALARM_RECOVERY)) {
                message.setCode(EnumHandler.ALARM_RECOVERY.getType());
                message.setMessageName(EnumHandler.ALARM_RECOVERY.name());
                iMessageStrategyManagerService.handle(message);
            } else if (MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.BREAK_DOWN_RECOVERY)) {
                message.setCode(EnumHandler.BREAK_DOWN_RECOVERY.getType());
                message.setMessageName(EnumHandler.BREAK_DOWN_RECOVERY.name());
                iMessageStrategyManagerService.handle(message);
            } else if (MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.REAL_DATA)) {
                message.setCode(EnumHandler.REAL_DATA.getType());
                message.setMessageName(EnumHandler.REAL_DATA.name());
                iMessageStrategyManagerService.handle(message);
            } else if (MessageUtils.compere(MessageUtils.getId(bytes), MessageUtils.MALIGNANT_ALARM)) {
                message.setCode(EnumHandler.MALIGNANT_ALARM.getType());
                message.setMessageName(EnumHandler.MALIGNANT_ALARM.name());
                iMessageStrategyManagerService.handle(message);
            }/*else if (MessageUtils.getShortId(MessageUtils.getId(bytes)) == MessageUtils.BAD_LOAD) {
                message.setCode(EnumHandler.BAD_LOAD.getType());
                message.setMessageName(EnumHandler.BAD_LOAD.name());
                iMessageStrategyManagerService.handle(message);
            }*/else{
                log.info("消息为找到,id:{}",MessageUtils.getShortId(bytes));
                message.setCode(EnumHandler.UNKNOW_ALARM.getType());
                message.setMessageName(EnumHandler.UNKNOW_ALARM.name());
                iMessageStrategyManagerService.handle(message);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                dataInputStream.close();
               // socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public short bytesToShort(byte[] bytes) {
        return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getShort();
    }
}
